--编写者：alt
--功能介绍：料理food模板
--如何添加:
--[[
1.这个文件直接写在prefabs文件里面 然后在modmain里面的PrefabFiles里面加上文件名就行
2.修改foods表里面的数据
 ]]
--资源注意:
--[[
1.食物的动画包制作需要注意：（一句话说就是build:prefab名 bank:cook_pot_food 通道:prefab名 并且通道里面的图片做一个不是叫idle的动画）
build名字使用食物的prefab名(打包出来是xxx.zip)
bank名字需要设置为cook_pot_food（也就是软件里面需要修改的那个名字）
anim 需要做一个动画 不然打包不了 这个动画在游戏里面是不会使用的 命名随便写 不写idle就行  建议软件里面的动画命名为食物英文名
制作动画的时候需要创建一个通道（也就是文件夹 和预制物prefab同名就行） 把图片放进去 然后在软件里面拖出来随便做个动画就行
因为游戏内会使用OverrideSymbol替换贴图 所以图片的红点位置很重要 这个需要你在sp里面修改一下 
红点位置决定了食物地面动画和地面接触的位置和食物在锅里面的时候显示的位置
2.食物的贴图名和食物预制物同名 然后正常注册就行
推荐使用example_assets一键注册贴图
]]
--其余注意事项:
--[[
1.my_mod_specialid需要修改
]]
--foods的参数详解:
--[[
1.overridebuild: 加载的动画包的名字 克雷也会用这个判断替换锅的图
2.test:食谱测试函数
3.priority: 食谱优先级 如果你确定你的食物设计合理 那就可以无脑拉高。如果你的食物很容易做出 建议合理设置！
3.foodtype: 食物类型
4.health hunger sanity：食物三维属性
5.perishtime: 食物腐烂时间 单位/s 1day==8*60
6.cooktime: 食物制作时间  --单位/s
7.common_postinit--双端初始化执行的函数
8.master_postinit --主机初始化执行的函数
9.floater--漂浮在水上的数据 不会改就不用管
10.card_def--食谱卡 不想要可以不写
11.nospiced --是否不可以调味
12.isMasterfood = true--是否大厨专属
 ]]
local my_mod_specialid = "my_mod_specialid" --这个id需要修改
local foods = {}
foods.prefab = {
	overridebuild = "examplefood", --加载的动画包的名字 建议和prefab同名

	test = function(cooker, names, tags)
		return (names.butterflywings or names.moonbutterflywings) and not tags.meat and
			tags.veggie and tags.veggie >= 0.5
	end,
	priority = 99,
	foodtype = FOODTYPE.VEGGIE,
	health = 1,
	hunger = 1,
	sanity = 1,
	perishtime = 8 * 8 * 60,
	cooktime = 2,
	common_postinit = function(inst)
	end,
	master_postinit = function(inst)
	end,
}
for k, v in pairs(foods) do
	v.my_mod_specialid = my_mod_specialid
	--floater = { "small", 0.05, 0.7 },--漂浮在水上的数据 不会改就不用管
	--card_def = { ingredients = { { "butterflywings", 1 }, { "carrot", 2 }, { "berries", 1 } } }, --食谱卡 不想要可以不写
end
for k, v in pairs(foods) do
	v.name = k
	v.weight = v.weight or 1
	v.priority = v.priority or 0

	v.cookbook_category = "mod"
end
require("cooking")
for k, recipe in pairs(foods) do
	if not recipe.isMasterfood then
		AddCookerRecipe("cookpot", recipe, true)
		AddCookerRecipe("archive_cookpot", recipe, true)
	end
	AddCookerRecipe("portablecookpot", recipe, true)

	if recipe.card_def then
		AddRecipeCard("cookpot", recipe)
	end
end

local spicedfoods = require("spicedfoods")
GenerateSpicedFoods(foods)
for _, v in pairs(spicedfoods) do
	if v.my_mod_specialid == my_mod_specialid and not v.nospiced then
		v.official = false
		AddCookerRecipe("portablespicer", v, true)
	end
end

local prefabs =
{
	"spoiled_food",
}

local function MakePreparedFood(data)
	local foodassets =
	{
		--Asset("ANIM", "anim/cook_pot_food.zip"),
		--Asset("INV_IMAGE", data.name),
	}

	if data.overridebuild then
		table.insert(foodassets, Asset("ANIM", "anim/" .. data.overridebuild .. ".zip"))
	end

	local spicename = data.spice ~= nil and string.lower(data.spice) or nil
	if spicename ~= nil then
		table.insert(foodassets, Asset("ANIM", "anim/spices.zip"))
		table.insert(foodassets, Asset("ANIM", "anim/plate_food.zip"))
		table.insert(foodassets, Asset("INV_IMAGE", spicename .. "_over"))
	end

	local foodprefabs = prefabs

	local function DisplayNameFn(inst)
		return subfmt(STRINGS.NAMES[data.spice .. "_FOOD"], { food = STRINGS.NAMES[string.upper(data.basename)] })
	end

	local function fn()
		local inst = CreateEntity()

		inst.entity:AddTransform()
		inst.entity:AddAnimState()
		inst.entity:AddNetwork()

		MakeInventoryPhysics(inst)

		local food_symbol_build = nil
		if spicename ~= nil then
			inst.AnimState:SetBuild("plate_food")
			inst.AnimState:SetBank("plate_food")
			inst.AnimState:OverrideSymbol("swap_garnish", "spices", spicename)

			inst:AddTag("spicedfood")

			inst.inv_image_bg = { image = (data.basename or data.name) .. ".tex" }
			inst.inv_image_bg.atlas = GetInventoryItemAtlas(inst.inv_image_bg.image)

			food_symbol_build = data.overridebuild or "cook_pot_food"
		else
			inst.AnimState:SetBuild(data.overridebuild or "cook_pot_food")
			inst.AnimState:SetBank("cook_pot_food")
		end

		inst.AnimState:PlayAnimation("idle")
		inst.AnimState:OverrideSymbol("swap_food", data.overridebuild or "cook_pot_food",
			data.overridefilename or data.basename or data.name)

		inst:AddTag("preparedfood")

		if data.tags ~= nil then
			for i, v in pairs(data.tags) do
				inst:AddTag(v)
			end
		end

		if data.basename ~= nil then
			inst:SetPrefabNameOverride(data.basename)
			if data.spice ~= nil then
				inst.displaynamefn = DisplayNameFn
			end
		end

		if data.floater ~= nil then
			MakeInventoryFloatable(inst, data.floater[1], data.floater[2], data.floater[3])
		else
			MakeInventoryFloatable(inst)
		end

		if data.common_postinit ~= nil then
			data.common_postinit(inst)
		end

		inst.entity:SetPristine()

		if not TheWorld.ismastersim then
			return inst
		end

		inst.food_symbol_build = food_symbol_build or data.overridebuild
		inst.food_basename = data.basename

		inst:AddComponent("edible")
		inst.components.edible.healthvalue = data.health
		inst.components.edible.hungervalue = data.hunger
		inst.components.edible.foodtype = data.foodtype or FOODTYPE.GENERIC
		inst.components.edible.secondaryfoodtype = data.secondaryfoodtype or nil
		inst.components.edible.sanityvalue = data.sanity or 0
		inst.components.edible.temperaturedelta = data.temperature or 0
		inst.components.edible.temperatureduration = data.temperatureduration or 0
		inst.components.edible.nochill = data.nochill or nil
		inst.components.edible.spice = data.spice
		inst.components.edible:SetOnEatenFn(data.oneatenfn)

		inst:AddComponent("inspectable")
		inst.wet_prefix = data.wet_prefix

		inst:AddComponent("inventoryitem")
		if data.OnPutInInventory then
			inst:ListenForEvent("onputininventory", data.OnPutInInventory)
		end

		if spicename ~= nil then
			inst.components.inventoryitem:ChangeImageName(spicename .. "_over")
		elseif data.basename ~= nil then
			inst.components.inventoryitem:ChangeImageName(data.basename)
		elseif data.imagename ~= nil then
			inst.components.inventoryitem:ChangeImageName(data.imagename)
		end

		inst:AddComponent("stackable")
		inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM

		if data.perishtime ~= nil and data.perishtime > 0 then
			inst:AddComponent("perishable")
			inst.components.perishable:SetPerishTime(data.perishtime)
			inst.components.perishable:StartPerishing()
			inst.components.perishable.onperishreplacement = "spoiled_food"
		end

		MakeSmallBurnable(inst)
		MakeSmallPropagator(inst)
		MakeHauntableLaunchAndPerish(inst)

		inst:AddComponent("bait")

		inst:AddComponent("tradable")

		if data.master_postinit ~= nil then
			data.master_postinit(inst)
		end

		return inst
	end
	return Prefab(data.name, fn, foodassets, foodprefabs)
end

local prefs = {}

for _, foodDef in pairs(foods) do
	table.insert(prefs, MakePreparedFood(foodDef))
end

for k, v in pairs(require("spicedfoods")) do
	if v.my_mod_specialid == my_mod_specialid and not v.nospiced then
		table.insert(prefs, MakePreparedFood(v))
	end
end

return unpack(prefs)
